// miceshot.cpp : Implementation of DLL Exports.

#include "stdafx.h"

bool cutTopLeft(LPPOINT psize, LPPOINT poffset)
{
	bool result = false;
	if (poffset->x < 0)
	if (::abs(poffset->x) < psize->x)
	{
		psize->x += poffset->x;
		poffset->x = 0;
		result = true;
	}

	if (poffset->y < 0)
	if (::abs(poffset->y) < psize->y)
	{
		psize->y += poffset->y;
		poffset->y = 0;
		result = true;
	}

	return result;
}

void CreateBitmapInfoStruct(const BITMAP & bmp, PBITMAPINFO pbmi)
{ 
	//	unsigned clr2bit[] = { 1, 4, 4, 8, 16, };
    // Convert the color format to a count of bits.  
    WORD    cClrBits = (WORD) (bmp.bmPlanes * bmp.bmBitsPixel); 
    if (cClrBits == 1) 
        cClrBits = 1; 
    else if (cClrBits <= 4) 
        cClrBits = 4; 
    else if (cClrBits <= 8) 
        cClrBits = 8; 
    else if (cClrBits <= 16) 
        cClrBits = 16; 
    else if (cClrBits <= 24) 
        cClrBits = 24; 
    else cClrBits = 32; 

    // Allocate memory for the BITMAPINFO structure. (This structure  
    // contains a BITMAPINFOHEADER structure and an array of RGBQUAD  
    // data structures.)  

    // Initialize the fields in the BITMAPINFO structure.  

    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pbmi->bmiHeader.biWidth = bmp.bmWidth; 
    pbmi->bmiHeader.biHeight = bmp.bmHeight; 
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
    if (cClrBits < 24) pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 

    // If the bitmap is not compressed, set the BI_RGB flag.  
    pbmi->bmiHeader.biCompression = BI_RGB;

    // Compute the number of bytes in the array of color  
    // indices and store the result in biSizeImage.  
    // The width must be DWORD aligned unless the bitmap is RLE compressed. 
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                  * pbmi->bmiHeader.biHeight; 
    // Set biClrImportant to 0, indicating that all of the  
    // device colors are important.  
     pbmi->bmiHeader.biClrImportant = 0;

}

bool NotAtlGetErrorDescription(DWORD acode, LPCWSTR acontext, BSTR * retval)
{
	if (!acontext) acontext = L"HRESULT 0x%8.8x";

	ATL::CComBSTR thestr;
	DWORD	dwLangId(0);
	LPWSTR	szMessage = NULL;
	DWORD	thecount = ::FormatMessageW(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,	//	Location of the message definition. 
			acode,	//	Message identifier for the requested message. 
			dwLangId,
			(LPTSTR) &szMessage,
			0,
			NULL);

	if (szMessage) 
	{
		if (thecount) thestr = szMessage;
		::LocalFree(szMessage);
	}

	{
		WCHAR thebuf[MAX_PATH + 1];
			(* thebuf) = 0;
		int thecount = ::wnsprintfW( thebuf, MAX_PATH, acontext, acode );
		if (* thebuf)
		{
			if (thecount > MAX_PATH) thecount = MAX_PATH;
				thebuf[thecount] = 0;
			thestr.Append(thebuf);
		}
	}

	if (retval)
	if (thestr.Length() >> 1)
	{
		(* retval) = thestr.Detach();
		return true;
	}

	return false;
}
